<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * @package		Adminpro
 * @author		Tran Hoang Thien (thienhb12@gmail.com)
 * @copyright   PHP TEAM
 * @link		http://phpandmysql.net
 * @since		Version 1.0
 * @phone       0944418192
 *
 */

class Login extends MY_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('muser');
		$this->load->library('session');
	}

	public function index(){
		$data['title'] = 'Login anime';
		if($this->input->post('type') != NULL){
			$user  = fillter($this->input->post('username'));
			$pass  = fillter($this->input->post('password'));
			$check = $this->muser->check_login($user,$pass);
			if($check == FALSE ){
				echo 'false';
			}elseif ($check === 'notactive') {
				echo 'notactive';
			}elseif ($check === 'notlogin') {
				echo 'notlogin';
			}
			else{
				echo 'true';
				$session = array(
					'session_user'      => $check['username'],
					'session_user_id'   => $check['iduser'],
					'session_level'     => $check['usergroup'],
					'session_fullname'  => $check['user_fullname'],
					'session_avarta'    => $check['avatar'],
					'session_logintime' => time(),
				);
				$this->session->set_userdata($session);
				$this->muser->save($check['iduser'],array('status_login' => 1));
			}	
		}else{
			$data['error'] = '';
			$this->load->view('login',$data);
		}
	}

	public function logout(){
		$data = array('session_user'=>'','session_user_id' =>'','session_level'=>'','session_fullname'=>'','session_avarta'=>'');
		$id = $this->session->userdata('session_user_id');
		$this->muser->save($id,array('status_login' => 0));
		$this->session->unset_userdata($data);
		redirect(base_url().'admin/login');
	}
}
